//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.rightin.reader.hf.myhf;

import android.util.Log;
import com.rightin.reader.hf.rfidenum.EnumModuleState;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class HfBaseSocket {
    private Socket clientSocket = null;
    private OutputStream outStream = null;
    private InputStream inputStream = null;
    private HfBaseSocket.ReceiveThread mReceiveThread = null;
    private Thread clientThread = null;
    private String _ip;
    private int _port;
    private HfBaseSocket.OnBaseSocketListener baseSocketListener = null;
    private EnumModuleState moduleState;

    public HfBaseSocket() {
        this.moduleState = EnumModuleState.IDLE;
    }

    public void setOnBaseSocketListener(HfBaseSocket.OnBaseSocketListener _baseSocketListener) {
        this.baseSocketListener = _baseSocketListener;
    }

    public void open(String ip, int port) {
        try {
            this._ip = ip;
            this._port = port;
            this.clientSocket = new Socket();
            this.clientSocket.connect(new InetSocketAddress(ip, port), 5000);
            this.moduleState = EnumModuleState.WORK;
            this.clientThread = new Thread(new HfBaseSocket.ReceiveThread(this.clientSocket));
            this.clientThread.start();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

    }

    public void reopen() {
        this.open(this._ip, this._port);
    }

    public void close() {
        try {
            if (this.clientThread != null) {
                this.clientThread.interrupt();
            }

            this.clientSocket.close();
        } catch (IOException var2) {
            var2.printStackTrace();
        }

    }

    private void startThread() {
        if (this.clientThread == null) {
            this.moduleState = EnumModuleState.WORK;
            this.clientThread = new Thread(new HfBaseSocket.ReceiveThread(this.clientSocket));
            this.clientThread.start();
        }

    }

    private void stopThread() {
        this.moduleState = EnumModuleState.IDLE;
        if (this.clientThread != null) {
            this.clientThread.interrupt();
        }

    }

    public void writeBytes(byte[] buffer) {
        try {
            OutputStream outStream = this.clientSocket.getOutputStream();
            outStream.write(buffer);
            this.baseSocketListener.onSendedHandler(this.clientSocket, buffer);
        } catch (IOException var3) {
            var3.printStackTrace();
        }

    }

    public byte[] readBytes() {
        byte[] buf = new byte[512];
        InputStream inStream = null;

        try {
            try {
                Thread.sleep(200L);
                inStream = this.clientSocket.getInputStream();
                int size = inStream.available();
                if (size > 0) {
                    int ilen = inStream.read(buf);
                    byte[] buffer = HfCommon.readBytes(buf, 0, ilen);
                    this.baseSocketListener.onReceivedHandler(this.clientSocket, buffer);
                    return buffer;
                }
            } catch (IOException var11) {
                var11.printStackTrace();
            } catch (InterruptedException var12) {
                var12.printStackTrace();
            }

            return null;
        } finally {
            ;
        }
    }

    public int readBytes2(byte[] buffer) {
        BufferedReader is = null;

        try {
            is = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream(), "gbk"));
            char[] cbuf = new char[1024];
            int i = is.read(cbuf, 0, 1024);
            is.close();
            return i;
        } catch (IOException var5) {
            var5.printStackTrace();
            return 0;
        }
    }

    public void updateReadState(EnumModuleState state) {
        this.moduleState = state;
    }

    private class ReceiveThread extends Thread {
        private InputStream inStream = null;
        private byte[] buf = new byte[512];
        private String str = null;
        private Socket clientSocket;

        ReceiveThread(Socket s) {
            try {
                this.clientSocket = s;
                this.inStream = s.getInputStream();
                HfBaseSocket.this.inputStream = this.inStream;
            } catch (IOException var4) {
                var4.printStackTrace();
            }

        }

        public void run() {
            super.run();

            try {
                try {
                    while(!this.isInterrupted()) {
                        if (HfBaseSocket.this.moduleState == EnumModuleState.PAUSE) {
                            Thread.sleep(10L);
                        } else if (HfBaseSocket.this.moduleState != EnumModuleState.IDLE) {
                            int size = this.inStream.available();
                            if (size > 0) {
                                int ilen = this.inStream.read(this.buf);
                                byte[] buffer = HfCommon.readBytes(this.buf, 0, ilen);
                                HfBaseSocket.this.baseSocketListener.onReceivedHandler(this.clientSocket, buffer);
                            }
                        }
                    }
                } catch (Exception var7) {
                    Log.i("HfReader Error", var7.getMessage());
                }

            } finally {
                ;
            }
        }
    }

    public interface OnBaseSocketListener {
        void onSendedHandler(Socket var1, byte[] var2);

        void onReceivedHandler(Socket var1, byte[] var2);

        void onErrorHandler(Socket var1, int var2, String var3);
    }
}
